package cn.iocoder.yudao.module.system.controller.admin.copying;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.copying.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.copying.CopyingDO;
import cn.iocoder.yudao.module.system.service.copying.CopyingService;

@Tag(name = "管理后台 - 文案")
@RestController
@RequestMapping("/system/copying")
@Validated
public class CopyingController {

    @Resource
    private CopyingService copyingService;

    @PostMapping("/create")
    @Operation(summary = "创建文案")
    @PreAuthorize("@ss.hasPermission('system:copying:create')")
    public CommonResult<Long> createCopying(@Valid @RequestBody CopyingSaveReqVO createReqVO) {
        return success(copyingService.createCopying(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新文案")
    @PreAuthorize("@ss.hasPermission('system:copying:update')")
    public CommonResult<Boolean> updateCopying(@Valid @RequestBody CopyingSaveReqVO updateReqVO) {
        copyingService.updateCopying(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:copying:delete')")
    public CommonResult<Boolean> deleteCopying(@RequestParam("id") Long id) {
        copyingService.deleteCopying(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:copying:query')")
    public CommonResult<CopyingRespVO> getCopying(@RequestParam("id") Long id) {
        CopyingDO copying = copyingService.getCopying(id);
        return success(BeanUtils.toBean(copying, CopyingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文案分页")
    @PreAuthorize("@ss.hasPermission('system:copying:query')")
    public CommonResult<PageResult<CopyingRespVO>> getCopyingPage(@Valid CopyingPageReqVO pageReqVO) {
        PageResult<CopyingDO> pageResult = copyingService.getCopyingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CopyingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文案 Excel")
    @PreAuthorize("@ss.hasPermission('system:copying:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCopyingExcel(@Valid CopyingPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CopyingDO> list = copyingService.getCopyingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文案.xls", "数据", CopyingRespVO.class,
                BeanUtils.toBean(list, CopyingRespVO.class));
    }

}